package com.zadtek.jpsqishi.activity.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zadtek.jpsqishi.R;
import com.zadtek.jpsqishi.activity.OrderDetailActivity_;
import com.zadtek.jpsqishi.activity.common.Common;
import com.zadtek.jpsqishi.activity.data.OrderWaitData;
import com.zadtek.jpsqishi.views.CountDownTimer;

public class OrderWaitAdapter extends CommonAdapter<OrderWaitData> {
	private String formatAddress = "";
	private CountDownTimer timer;
	private SparseArray<CountDownTimer> timers;

	public OrderWaitAdapter(Context context, List<OrderWaitData> datas, int layoutId) {
		super(context, datas, layoutId);
		// TODO Auto-generated constructor stub
		timers = new SparseArray<>();
	}

	@Override
	public void convert(ViewHolders holder, final OrderWaitData t, int position) {
		TextView newtaskmomey = holder.getView(R.id.waitgood_money);
		TextView newtasktime = holder.getView(R.id.waitgood_time);
		TextView order_number = holder.getView(R.id.order_number);
		TextView extra_reward = holder.getView(R.id.extra_reward);
		TextView newtask_goods_getaddress = holder.getView(R.id.wait_goods_getaddress);
		TextView getgpsaddr = holder.getView(R.id.getgpsaddr);
		TextView orderStartTime = holder.getView(R.id.orderStartTime);
		TextView orderEndTime = holder.getView(R.id.orderEndTime);
		TextView order_end_time_stamp = holder.getView(R.id.order_end_time_stamp);
		ProgressBar progress = holder.getView(R.id.progress);

		newtaskmomey.setText("¥" + "" + t.getReturnpay());
		order_number.setText("订单编号：" + "" + t.getMain_id());
		extra_reward.setText(" +" + "" + t.getExtra_reward());

		orderStartTime.setText(t.getOrder_start_time());
		orderEndTime.setText(t.getOrder_end_time());
		progress.setProgress((int) (t.getRate() * 100));


		int order_end_time_stamp1 = t.getOrder_end_time_stamp();
		//获取当前时间戳
		long timeStamp = System.currentTimeMillis() / 1000;

		CountDownTimer countDownTimer = timers.get(order_end_time_stamp.hashCode());
		if (countDownTimer != null) {
			countDownTimer.cancel();
		}

		long s1 = order_end_time_stamp1 - timeStamp;
		long s2 = timeStamp - order_end_time_stamp1;
		if (s1 > 0) {
			countdown(s1, order_end_time_stamp, false, s2);
		} else {
			if (s1 + 36000 > 0) {
				countdown((s1 + 36000), order_end_time_stamp, true, s2);
			} else {
				order_end_time_stamp.setText("已超时");
			}
		}

		if (!t.getAdd_time().equals("") || t.getAdd_time() != null) {
			newtasktime.setText(Common.getStrTime(t.getAdd_time()));
		}

		try {
			String address_road = t.getAddress_road();
			if (address_road == null || address_road.equals("") || address_road.equals("null")) {
				String addrss = t.getGps_addrs();
				if (addrss != null && !addrss.equals("")) {
					getgpsaddr.setText(addrss); // 根据经纬度反查地址
					getgpsaddr.setVisibility(View.VISIBLE);
				} else {
					getgpsaddr.setVisibility(View.GONE);
				}
			} else {
				getgpsaddr.setText(address_road); // 根据经纬度反查地址
				getgpsaddr.setVisibility(View.VISIBLE);
			}
		} catch (Exception e) {
		}

		newtask_goods_getaddress.setText(t.getSendaddress());
		LinearLayout submitthread = holder.getView(R.id.submitthread);
		ListView ItemList = holder.getView(R.id.waitgood_itemlist);
		OrderWaitItemAdapter itemadapter = new OrderWaitItemAdapter(mContext,
				t.getOrderItemList(), R.layout.waitgoodlist_itemlist_item,
				t.getMain_id());
		ItemList.setAdapter(itemadapter);

		submitthread.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (t.getMain_id() != null && !t.getMain_id().equals("")) {
					Intent intent = new Intent(mContext, OrderDetailActivity_.class);
					intent.putExtra("mainid", t.getMain_id());
					intent.putExtra("status", "wait");
					mContext.startActivity(intent);
				}
			}
		});
	}

	private void countdown(final long s, final TextView tv, final boolean isTimeOut, final long s2) {
		/** 倒计时s秒，一次1秒 */
		CountDownTimer timer = new CountDownTimer(s * 1000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
				// TODO Auto-generated method stub
				long ss;
				if (isTimeOut) {
					ss = s2 + s - millisUntilFinished / 1000;
				} else {
					ss = millisUntilFinished / 1000;
				}
				if (ss < 60) {
					if (isTimeOut) {
						tv.setText("超时" + ss + "秒");
					} else {
						tv.setText("剩余" + ss + "秒");
					}
				} else if (millisUntilFinished < 3600) {
					if (isTimeOut) {
						tv.setText("超时" + ss / 60 + "分" + ss % 60 + "秒");
					} else {
						tv.setText("剩余" + ss / 60 + "分" + ss % 60 + "秒");
					}
				} else {
					if (isTimeOut) {
						tv.setText("超时" + ss / 3600 + "时" + ss % 3600 / 60 + "分" + ss % 60 + "秒");
					} else {
						tv.setText("剩余" + ss / 3600 + "时" + ss % 3600 / 60 + "分" + ss % 60 + "秒");
					}
				}
			}

			@Override
			public void onFinish() {
				if (isTimeOut) {
					tv.setText("已超时");
				} else {
					countdown(36000, tv, true, 0);
				}
			}
		}.start();

		timers.put(tv.hashCode(), timer);
	}
}
